﻿Namespace DotNetNuke.Modules.ActiveForums.Controls
    Public Class TopicViewer
        Public Property PortalId() As Integer = -1
        Public Property ModuleId() As Integer = -1
        Public Property TabId() As Integer = -1
        Public Property ForumIds() As String = String.Empty
        Public Property ForumGroupId() As Integer = -1
        Public Property ParentForumId() As Integer = -1
        Public Property TopicId() As Integer = -1
        Public Property Topic() As String = String.Empty
        Public Property Template() As String = String.Empty
        Public Property HeaderTemplate() As String = String.Empty
        Public Property FooterTemplate() As String = String.Empty
        Public Property ForumUser() As User
        Public Property PageIndex() As Integer = 1
        Public Property PageSize() As Integer = 20
        Public Property ItemCss() As String = "aftb-topic"
        Public Property AltItemCSS() As String = "aftb-topic-alt"
        Public Property TimeZoneOffset() As Integer = 0
        Public Function Render() As String
            Dim sb As New Text.StringBuilder()
            Dim db As New Data.Topics
            Dim i As Integer = 0

            Using dr As IDataReader = db.TopicWithReplies(PortalId, TopicId, PageIndex, PageSize)
                While dr.Read()
                    Template = ParseTopic(dr, Template)
                End While
                dr.NextResult()
                Dim rtemplate As String = TemplateUtils.GetTemplateSection(Template, "[REPLIES]", "[/REPLIES]")
                While dr.Read()
                    sb.Append(ParseReply(dr, rtemplate))
                End While
                dr.Close()
            End Using
            Template = TemplateUtils.ReplaceSubSection(Template, sb.ToString, "[REPLIES]", "[/REPLIES]")
            Return Template
        End Function
        Private Function ParseTopic(ByVal row As IDataRecord, ByVal tmp As String) As String
            tmp = ParseDataRow(row, tmp)
            Dim cUtils As New ControlUtils
            tmp = tmp.Replace("[TOPICURL]", cUtils.TopicURL(row, TabId, ModuleId))
            tmp = tmp.Replace("[FORUMURL]", cUtils.ForumURL(row, TabId, ModuleId))
            tmp = tmp.Replace("[TOPICSTATE]", cUtils.TopicState(row))
            Return tmp
        End Function
        Private Function ParseReply(ByVal row As IDataRecord, ByVal tmp As String) As String
            Return ParseDataRow(row, tmp)
        End Function
        Private Function ParseDataRow(ByVal row As IDataRecord, ByVal tmp As String) As String
            Try
                For i As Integer = 0 To row.FieldCount - 1
                    Dim name As String = row.GetName(i)
                    Dim k As String = "[" & name.ToUpperInvariant & "]"
                    Dim value As String = row(i).ToString
                    Select Case row(i).GetType().ToString
                        Case "System.DateTime"
                            value = Utilities.GetDate(CDate(row(i).ToString), ModuleId, TimeZoneOffset)
                    End Select
                    tmp = tmp.Replace(k, value)
                Next
                
                'tmp = tmp.Replace("[AVATAR]", "<span style=""background-image:url('/desktopmodules/activesocial/profilepic.ashx?PortalId=" & PortalId.ToString & "&uid=" & row("LastAuthorId").ToString & "&h=26&w=26');""></span>")
                Return tmp
            Catch ex As Exception
                Return ex.Message
            End Try

        End Function
    End Class
End Namespace